/**
 * @class   wosh::gui::WidgetFileSelector
 * @brief   
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: WidgetFileSelector.h 2184 2010-03-20 13:19:23Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WidgetFileSelector_H__
 #define __WOSH_Gui_WidgetFileSelector_H__

 #include <core/URI.h>
 #include <core/FileSystem.h>

 #include <framework/gui/IWidget.h>
 #include <framework/gui/WoshTableModel.h>

 #include "ui_WidgetFileSelector.h"

 #include <QFileSystemModel>


namespace Ui {
    class WidgetFileSelector;
}

namespace wosh {
 namespace gui {



class WidgetFileSelector :  public QDialog  {

	Q_OBJECT

	public:

		WidgetFileSelector(QWidget *parent = 0) : QDialog(parent)
		 {
			this->m_ui = new Ui::WidgetFileSelector();
			this->m_ui->setupUi(this);

			this->dirModel = new QFileSystemModel();
			this->m_ui->treeView->setModel(this->dirModel);

			QObject::connect(this->m_ui->tBtnTool, SIGNAL(clicked()), this, SLOT(btnClicked()));

			QObject::connect(this->m_ui->treeView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(doubleClicked(const QModelIndex&)));
			QObject::connect(this->m_ui->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(clicked(const QModelIndex&)));
			QObject::connect(this->m_ui->treeView, SIGNAL(expanded(const QModelIndex&)), this, SLOT(expanded(const QModelIndex&)));

		 }

		virtual ~WidgetFileSelector() {

		 }


	public:

		const FilePath& getItemPath() const			{ return this->itemPath; }
		FilePath& getItemPath()						{ return this->itemPath; }

		const FilePath& getRootPath() const			{ return this->rootPath; }
		FilePath& getRootPath()						{ return this->rootPath; }


	public:

		void setRootPath( const std::string& path )
		{
			this->rootPath.set( path );

			this->m_ui->lineEdit->setText( this->rootPath.getPath().c_str() );
			this->m_ui->lineEdit->setToolTip( this->rootPath.toPathLocal().c_str() );

			QModelIndex indexRoot = this->dirModel->setRootPath( QString(this->rootPath.toPathLocal().c_str()) );
			this->m_ui->treeView->setRootIndex( indexRoot );
			this->m_ui->treeView->resizeColumnToContents(0);
		 }

	protected:

		void changeEvent(QEvent *e)
		 {
			QWidget::changeEvent(e);
			switch (e->type()) {
				case QEvent::LanguageChange:
				m_ui->retranslateUi(this);
				break;
			default: break;
			}
		 }

		void updateURI()
		 {
//			this->targetURI.fromString(this->m_ui->lineEdit->text().toLatin1().data());
		 }

	public slots:

		virtual void refreshAll() {
		 }


	protected slots:

		void editor_editingFinished() {
			updateURI();
			emit changedURI( this->targetURI );
		 }

		void btnClicked() {
			QString path = this->m_ui->lineEdit->text();
			setRootPath( path.toLatin1().data() );
		 }


		void clicked( const QModelIndex & index ) {
			std::string path = std::string( this->dirModel->filePath(index).toLatin1().data() );
			FilePath::toLocalPath(path);
			this->itemPath.fromLocalPath( path );
			accepted();
		 }

		void doubleClicked( const QModelIndex & index ) {
			clicked( index );
			accept();
		 }

		void expanded( const QModelIndex& ) {
			this->m_ui->treeView->resizeColumnToContents(0);
		 }

	signals:

		void changedURI( const URI& uri );


	protected:

		QFileSystemModel* dirModel;

		FilePath rootPath;
		FilePath itemPath;


	//	std::string itemPath; // path to file (to combine with rootpath)
		std::string itemPathLocal;


		URI targetURI;

	private:
		Ui::WidgetFileSelector *m_ui;


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_WidgetFileSelector_H__
